package com.suntine.familytree.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @corporation 
 * @author suntine
 * @creation 2011-4-14 下午04:00:25
 * @description 
 */
public class RefUtils {

	/*
	public P simpleCopy (P p,Q q){
		
		Map map  = new HashMap();
		try{
			Field arr_A_Field[] = q.getClass().getDeclaredFields();
			Field arr_B_Field[] = p.getClass().getDeclaredFields();
			for(Field qField : arr_A_Field){
//				qField.setAccessible(true);
				String fieldName = qField.getName();
				String newFieldName=fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);  
                Method method=q.getClass().getDeclaredMethod("get"+newFieldName); 
				if(method.invoke(q)!=null){
					map.put(fieldName,method.invoke(q));
				}
			}
			for(Field pField : arr_B_Field){
				String fieldName = pField.getName();
				String newFieldName=fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);  
                Method method=p.getClass().getDeclaredMethod("set"+newFieldName);
				if(map.get(fieldName)!=null){
					method.invoke(p,map.get(fieldName));
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return p;
	}*/
	
	
	/**
	 * @author suntine
	 * @param a
	 * @param b
	 * @return b
	 */
	static public Object simpleCopyA2B (Object a, Object b){
		
		Map<String,String> map  = new HashMap<String,String>();
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
			Class a_Class = a.getClass();
			Class b_Class = b.getClass();
			Field arr_A_Field[] = a_Class.getDeclaredFields(); // 对象a 字段数组
			Method arr_A_Method[]= a_Class.getDeclaredMethods(); //对象a 方法数组
			Field arr_B_Field[] = b_Class.getDeclaredFields(); // 对象b 字段数组
			Method arr_B_Method[]= b_Class.getDeclaredMethods(); //对象b 方法数组
			
			for(Field a_Field : arr_A_Field){
				
				String fieldName = a_Field.getName();
				String fieldType = a_Field.getType().getName();
				String fieldGetName="get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);  
				if(checkMethodExist(arr_A_Method,fieldGetName)){
					
					Method method = a_Class.getDeclaredMethod(fieldGetName);
					Object fieldValue ; 
					fieldValue = method.invoke(a);
					if(fieldValue!=null){
						if("Date".equals(fieldType)){
							map.put(fieldName,sdf.format(fieldValue));
						}else{
							map.put(fieldName,fieldValue.toString());
						}
					}
					
				}
			}
			
			for(Field b_Field : arr_B_Field){
				
//				b_Field.setAccessible(true);
				String fieldName = b_Field.getName();
				String fieldType = b_Field.getType().getName();
				String fieldSetName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); 
				if(checkMethodExist(arr_B_Method,fieldSetName)){
					
					Method method = b_Class.getDeclaredMethod(fieldSetName,b_Field.getType());
					String fieldValue = map.get(fieldName) ; 
					if(fieldValue!=null){
						if("Date".equals(fieldType)){
							method.invoke(b,sdf.parse(fieldValue));
						}else if ("Integer".equals(fieldType) || "int".equals(fieldType)) { 
							method.invoke(b,Integer.parseInt(fieldValue));
						}else if("Long".equalsIgnoreCase(fieldType)){
							method.invoke(b,Long.parseLong(fieldType));
						}else if("Float".equalsIgnoreCase(fieldType)){
							method.invoke(b,Float.parseFloat(fieldType));
						}else if("Double".equalsIgnoreCase(fieldType)){
							method.invoke(b,Double.parseDouble(fieldValue));
						}else if("Boolean".equalsIgnoreCase(fieldType)){
							method.invoke(b,Boolean.parseBoolean(fieldValue));
						}else{
							method.invoke(b,fieldValue);
						}
					}
					
				}
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}
		return b;
	}
	
	 public static boolean checkMethodExist(Method[] methods, String metName) {  
	        for (Method met : methods) {  
	            if (metName.equals(met.getName())) {  
	                return true;  
	            }  
	        }  
	        return false;  
    }  
}
